import hashlib
from rest_framework.response import Response
import os
def toMd5(str):
    """
    md5 加密
    :param str:
    :param salt:
    :return:
    """
    # satl是盐值，默认是123456
    md = hashlib.md5()  # 构造一个md5对象
    md.update(str.encode())
    res = md.hexdigest()
    return res

def saveFile(self,request,savepath):
    data = []
    files = request.FILES.getlist('file')
    try:
        for i in files:
            position = os.path.join(savepath, i.name)
            storage = open(position, 'wb+')  # 打开存储文件
            for chunk in i.chunks():  # 分块写入文件
                storage.write(chunk)
                storage.close()
            newname = toMd5(str(i.name).split('.')[0])
            os.rename(savepath+'%s'%i.name, savepath+'%s.%s'%(newname,'pdf'))
            data.append(newname)
    except Exception as e:
        return Response({'code': 4000, 'msg': '存储出错'})   
    return data